package ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseFragment;
import com.fy.fyzf.bean.AreaListBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.DemandListBean;
import com.fy.fyzf.bean.DemanddetailBean;
import com.fy.fyzf.bean.IsInfoBean;
import com.fy.fyzf.bean.MyRecommendBean;
import com.fy.fyzf.presenter.DemandPresenter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.TabLayoutUtils;
import com.fy.fyzf.view.DemandCallBack;
import com.google.android.material.tabs.TabLayout;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import ui.activity.IssueDemandActivity;
import ui.activity.SelfInfoActivity;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DemandFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DemandFragment extends BaseFragment<DemandPresenter> implements DemandCallBack {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.iv_issue)
    ImageView ivIssue;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private List<BaseFragment> fragmentList = new ArrayList<>();
    private List<String> titles = new ArrayList<>();

    public DemandFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment CheckWorkFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static DemandFragment newInstance(String param1, String param2) {
        DemandFragment fragment = new DemandFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    protected DemandPresenter createPresenter() {
        return new DemandPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_check_work;
    }


    @Override
    public void initView(View rootView) {
        super.initView(rootView);
        TabLayoutUtils.setTabLayoutWidth(20, 20, tablayout, getActivity());
        fragmentList.add(PeerFragment.newInstance("", ""));
        fragmentList.add(MyDemandFragment.newInstance("", ""));

        titles.add("同行合作");
        titles.add("我的需求");

        viewpager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                super.destroyItem(container, position, object);
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);

            }
        });

        tablayout.setupWithViewPager(viewpager);

        addDisposable(RxView.clicks(ivIssue)
                .throttleFirst(10, TimeUnit.SECONDS)
                .subscribe(o -> {
                    mPresenter.isInfo();
                }));
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void getDemandListSuccess(DemandListBean demandListBean) {

    }

    @Override
    public void issueDemandSuccess(BaseData data) {

    }

    @Override
    public void getAreaListSuccess(List<AreaListBean> listBeans) {

    }

    @Override
    public void getDamendDetailSuccess(DemanddetailBean demanddetailBean) {

    }

    @Override
    public void getMyDemandListSuccess(DemandListBean demandListBean) {

    }

    @Override
    public void deleteDemandSuccess(BaseData data, int i) {

    }

    @Override
    public void getMyRecommendSuccess(MyRecommendBean myRecommendBean) {

    }

    @Override
    public void isInfoSuccess(IsInfoBean isInfoBean) {
        if (isInfoBean.isIsPerfection()) {
            startActivity(new Intent(getActivity(), IssueDemandActivity.class));
        } else {
            startActivity(new Intent(getActivity(), SelfInfoActivity.class));
            AppUtils.showToast("请先去完善个人个人资料");

        }
    }

    @OnClick({R.id.text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text:
                break;
          /*  case R.id.iv_issue:

                break;*/
        }
    }

    //检测是否完善用户信息
    private void checkIsInfo(){

    }
}